package com.one.rope.mvp.web.service.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.one.rope.mvp.basecore.util.DateUtil;
import com.one.rope.mvp.basecore.util.MyBeanUtils;
import com.one.rope.mvp.web.business.dao.MemberInfoMapper;
import com.one.rope.mvp.web.business.dao.MemberIntentionLogMapper;
import com.one.rope.mvp.web.business.entity.MemberInfo;
import com.one.rope.mvp.web.business.entity.MemberIntentionLog;
import com.one.rope.mvp.web.business.entity.MemberPushTaskLog;
import com.one.rope.mvp.web.business.service.IMemberIntentionLogService;
import com.one.rope.mvp.web.business.service.IMemberPushTaskLogService;
import com.one.rope.mvp.web.common.auth.UserInfo;
import com.one.rope.mvp.web.common.auth.UserLoginUtil;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.web.common.dto.user.person.AddIntentionDTO;
import com.one.rope.mvp.web.common.dto.user.person.PersonIndexVO;
import com.one.rope.mvp.web.common.dto.user.person.RecommendListVO;
import com.one.rope.mvp.web.common.enums.MemberPushStatusEnum;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 人才端数据服务
 * <p>
 *
 * @author ： Weijian.Liu
 * @date ： 2020/3/15 22:04
 */
@Service
@Slf4j
public class PersonDataService {

  @Autowired
  MemberInfoMapper memberInfoMapper;

  @Autowired
  IMemberPushTaskLogService memberPushTaskLogService;

  @Autowired
  IMemberIntentionLogService memberIntentionLogService;

  /**
   * 获取人才端首页数据
   * @return
   */
  public ResponseObject<PersonIndexVO> personIndex() {
    UserInfo userInfo = UserLoginUtil.getUserInfo();
    MemberInfo memberInfo = memberInfoMapper.selectById(userInfo.getMemberInfo().getId());
    PersonIndexVO resultVO = new PersonIndexVO();
    resultVO.setName(memberInfo.getName());
    resultVO.setNoticeSwitch(memberInfo.getNoticeSwitch());
    resultVO.setSex(memberInfo.getSex());
    resultVO.setPageUrl(memberInfo.getPageUrl());

    List<MemberPushTaskLog> pushTaskLogList = memberPushTaskLogService.selectMemberPushList(userInfo.getMemberInfo().getId());
    resultVO.setJobRecommendNum(pushTaskLogList.size());
    return new ResponseObject<>(resultVO);
  }

  public ResponseObject<List<RecommendListVO>> recommendList() {
    List<RecommendListVO> reslutList = new ArrayList<>();
    UserInfo userInfo = UserLoginUtil.getUserInfo();
    QueryWrapper<MemberPushTaskLog> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("push_target", userInfo.getMemberInfo().getId());
    queryWrapper.eq("status", MemberPushStatusEnum.PUSH_SUCCESS.getCode());
    queryWrapper.eq("is_delete", 0);
    queryWrapper.orderByDesc("push_time");
    List<MemberPushTaskLog> pushTaskLogList = memberPushTaskLogService.list(queryWrapper);
    pushTaskLogList.stream().forEach(item -> {
      RecommendListVO recommendListVO = new RecommendListVO();
      MyBeanUtils.copyPropertiesIgnoreNull(item, recommendListVO);
      recommendListVO.setPushTime(DateUtil.dateToString(item.getPushTime(), DateUtil.DEFAULT_FORMAT));
      reslutList.add(recommendListVO);
    });
    return new ResponseObject<>(reslutList);
  }

  /**
   * 添加意向
   * @param requestDTO
   * @return
   */
  public ResponseObject addIntention(AddIntentionDTO requestDTO) {
    UserInfo userInfo = UserLoginUtil.getUserInfo();
    MemberIntentionLog memberIntentionLog = new MemberIntentionLog();
    memberIntentionLog.setIntentionTarget(requestDTO.getTargetId());
    memberIntentionLog.setIntentionUser(userInfo.getMemberInfo().getId());
    memberIntentionLogService.save(memberIntentionLog);
    return ResponseObject.successObject();
  }
}
